package cn.ihelloworld.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import cn.ihelloworld.bean.Tag;
import cn.ihelloworld.dao.ITagDao;
import cn.ihelloworld.dao.Pagation;

@Repository
public class TagDaoImpl implements ITagDao {

	@Autowired
	private SessionFactory sf;
	
	@Override
	public Tag get(String id) {
		// TODO Auto-generated method stub
		return (Tag) sf.getCurrentSession().get(Tag.class, id);
	}

	@Override
	public List<Tag> select(Tag t) {

		List<Tag> list=new ArrayList<Tag>();
		if(t!=null){
			Criteria criteria=sf.getCurrentSession().createCriteria(Tag.class);
			if(t.getId()!=null){
				criteria.add(Restrictions.eq("id", t.getId()));
			}
			if(t.getName()!=null){
				criteria.add(Restrictions.ilike("name", t.getName(),MatchMode.START));
			}
			list=criteria.list();
		}
		return list;
	
	}

	@Override
	public Pagation<Tag> selectForPage(Tag t, int currentPage, int pageSize) {
		Pagation<Tag> pagation=new Pagation<Tag>();
		int start=currentPage==0?0:(currentPage-1)*pageSize;
		int end=currentPage*pageSize;
		pagation.setStart(start);
		pagation.setEnd(end);
		List<Tag> list=new ArrayList<Tag>();
		if(t!=null){
			Criteria criteria=sf.getCurrentSession().createCriteria(Tag.class);
			if(t.getId()!=null){
				criteria.add(Restrictions.eq("id", t.getId()));
			}
			if(t.getName()!=null){
				criteria.add(Restrictions.eq("name", t.getName()));
			}
			Integer count=(Integer) criteria.setProjection(Projections.rowCount()).uniqueResult();
			pagation.setTotal(count);
			criteria.setProjection(null);
			criteria.setFirstResult(start);
			criteria.setMaxResults(end);
			list=criteria.list();
		}
		return null;
	}

	@Override
	public void saveOrUpdate(Tag t) {
		sf.getCurrentSession().saveOrUpdate(t);

	}

	@Override
	public void delete(String id) {
		sf.getCurrentSession().delete(id);

	}

	@Override
	public void delete(String[] ids) {
		StringBuffer sb=new StringBuffer("delete Tag where id in");
		sb.append("(");
		for(int i=0;i<ids.length;i++){
			sb.append("'");
			sb.append(ids[i]);
			sb.append("'");
			if(i!=ids.length-1){
				sb.append(",");
			}
		}
		sb.append(")");
		sf.getCurrentSession().createQuery(sb.toString()).executeUpdate();

	}

	@Override
	public List<Tag> selectHot(int num) {
		Query query=sf.getCurrentSession().createQuery("from Tag order by (readCount+useCount) desc");
		query.setMaxResults(num);
		return query.list();
	}

	@Override
	public void updateReadCountByName(String name) {
		// TODO Auto-generated method stub
		Query query=sf.getCurrentSession().createQuery("update Tag set readCount=readCount+1 where name=?");
		query.setParameter(0, name);
		query.executeUpdate();
	}

}
